package com.technology.education.ui.common.information

import androidx.lifecycle.MutableLiveData
import com.technology.education.data.SpKeys
import com.technology.education.ext.getAppViewModel
import com.technology.education.ext.postEventBus
import com.technology.education.ext.toast
import com.technology.education.network.apiService
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request

class CompleteWithdrawViewModel : BaseViewModel() {
    val name = StringObservableField()
    val account = StringObservableField()
    val result = MutableLiveData<Boolean>()

    /**
     * 0 为注册时候的完善,成功之后直接进入主页
     * 1 为提现时判断跳转,完成之后更改全局userInfo,直接结束页面
     */
    var type=0
    fun commit() {
        if (account.get().isEmpty()) {
            toast("请输入您的支付宝账号")
            return
        }
        if (name.get().isEmpty()) {
            toast("请输入您的姓名")
            return
        }
        request({
            apiService.updatePayInfo(
                hashMapOf(
                    "alipayName" to name.get(),
                    "alipayNo" to account.get()
                )
            )
        }, success = {
            if (type==1){
                getAppViewModel().userInfo.value?.apply {
                    alipayName=this@CompleteWithdrawViewModel.name.get()
                    alipayNo=account.get()
                }
                postEventBus(SpKeys.MODIFY_WITHDRAW_ACCOUNT,account.get())
            }
            result.postValue(true)
        }, error = {
            toast(it.errorMsg)
        }, isShowDialog = true)
    }

}